#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QVector>
#include <QTimer>
#include <QTime>



MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    data = new DataInfo;

    setupRealtimeDataDemo(ui->customPlot);
    ui->customPlot->replot();

    ui->checkBox_hum->setChecked(true);
    ui->checkBox_tem->setChecked(true);
//    connect(ui->checkBox_tem,SIGNAL(clicked()),this,SLOT(temClickedSlot()));
//    connect(ui->checkBox_hum,SIGNAL(clicked()),this,SLOT(humClickedSlot()));
    connect(ui->comboBox_num,SIGNAL(currentIndexChanged(int)),this,SLOT(numClickedSlot(int)));

}

MainWindow::~MainWindow()
{
    delete ui;
}

//画图初始化
void MainWindow::setupRealtimeDataDemo(QCustomPlot *customPlot)
{
    customPlot->addGraph(); // blue line
    customPlot->graph(0)->setPen(QPen(Qt::blue));
    customPlot->graph(0)->setName("hum");
    //customPlot->graph(0)->setBrush(QBrush(QColor(240, 255, 200)));
    //customPlot->graph(0)->setAntialiasedFill(false);
    customPlot->addGraph(); // red line
    customPlot->graph(1)->setPen(QPen(Qt::red));
    customPlot->graph(1)->setName("tem");
    //customPlot->graph(0)->setChannelFillGraph(customPlot->graph(1));


    customPlot->xAxis->setTickLabelType(QCPAxis::ltDateTime);
    customPlot->xAxis->setDateTimeFormat("hh:mm:ss");
    customPlot->xAxis->setAutoTickStep(false);
    customPlot->xAxis->setTickStep(60);
    customPlot->axisRect()->setupFullAxesBox();


    connect(&dataTimer, SIGNAL(timeout()), this, SLOT(realtimeDataSlot()));
    dataTimer.start(60); // Interval 0 means to refresh as fast as possible
    customPlot->legend->setVisible(true);

}


void MainWindow::realtimeDataSlot()
{
    //key的单位是秒
   // double key = QDateTime::currentDateTime().toMSecsSinceEpoch()/1000.0;
    //定义静态时间
    static QDateTime staticTime = QDateTime::fromString("2025-01-16 12:00:00", "yyyy-MM-dd hh:mm:ss");
    double key = staticTime.toMSecsSinceEpoch()/1000.0;

    //qsrand(QTime::currentTime().msec() + QTime::currentTime().second() * 10000);
    //产生两条曲线
    double value0;
    double value1;
    if (ui->checkBox_hum->isChecked())
        ui->customPlot->graph(0)->addData(key, value0);//添加数据1到曲线1

    if (ui->checkBox_tem->isChecked())
        ui->customPlot->graph(1)->addData(key, value1);//添加数据2到曲线2
    //删除8秒之前的数据。这里的8要和下面设置横坐标宽度的8配合起来
    //才能起到想要的效果，可以调整这两个值，观察显示的效果。
    ui->customPlot->graph(0)->removeDataBefore(key-60);
    ui->customPlot->graph(1)->removeDataBefore(key-60);

    //自动设定graph(1)曲线y轴的范围，如果不设定，有可能看不到图像
    //也可以用ui->customPlot->yAxis->setRange(up,low)手动设定y轴范围
    ui->customPlot->graph(0)->rescaleValueAxis();
    ui->customPlot->graph(1)->rescaleValueAxis(true);

    //这里的8，是指横坐标时间宽度为8秒，如果想要横坐标显示更多的时间
    //就把8调整为比较大到值，比如要显示60秒，那就改成60。
    //这时removeDataBefore(key-8)中的8也要改成60，否则曲线显示不完整。
    ui->customPlot->xAxis->setRange(key+0.25, 60, Qt::AlignRight);//设定x轴的范围
    ui->customPlot->replot();
}

void MainWindow::getHisTemData(int time, QString val)
{
    double value0 = val.toDouble();
}

void MainWindow::getHisHumData(int time, QString val)
{
    double value1 = val.toDouble();
}

void MainWindow::numClickedSlot(int index)
{
    QJsonObject json;
    json.insert("type",4);
    QJsonObject datajson;
    switch (index) {
    case 0:
        datajson.insert("key",200);
        break;
    case 1:
        datajson.insert("key",201);
        break;
    }
    //创建时间戳范围的数组
    QJsonArray limitArray;
    limitArray.append(1666482618);
    limitArray.append(1666582621);
    datajson.insert("limit", limitArray); // 将时间戳范围数组插入 data 对象
    json.insert("data",datajson);
    data->publishMessage(json);

}




void MainWindow::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);
    QPixmap map(":/new/prefix1/res/1.JPG");
    painter.drawPixmap(0,0,this->width(),this->height(),map);
}

